package com.project.ds.sqlprovider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;

import com.project.entity.activity.ActivityUser;
import com.project.entity.enumcommon.ActivityStatusEnum;

/**
 * 活动实体类Mybatis SQL语句工厂类
 * @author chenlz
 *
 */
public class ActivityUserMapperProvider extends BaseMapperProvider {
	/**
	 * 保存活动用户信息
	 * @author chen.liangzhen
	 * @time 2017年3月30日 下午3:32:47
	 * @version v0.1.0
	 * @param activityUser
	 * @return
	 */
    public String saveActivityUser(ActivityUser activityUser) {
    	StringBuilder sql = new StringBuilder();
    	sql.append("INSERT INTO B_ACTIVITY_USER (")
    		.append("activity_id,name,mobile,email,province,city,district,company,position,adept_field,honor,writings,intro,user_id,user_code) VALUES (")
    		.append(activityUser.getActivityId()+",'"+activityUser.getName()+"','"+activityUser.getMobile()+"','"+activityUser.getEmail()+"','"+activityUser.getProvince())
    		.append("','"+activityUser.getCity()+"','"+activityUser.getDistrict()+"','"+activityUser.getCompany()+"','"+activityUser.getPosition()+"','"+activityUser.getAdeptField())
    		.append("','"+activityUser.getHonor()+"','"+activityUser.getWritings()+"','"+activityUser.getIntro()+"','"+activityUser.getUserId()+"','"+activityUser.getUserCode()+"')");
    	return sql.toString();
    }
    
    /**
	 * 根据userId查询活动列表
	 * @author chen.liangzhen
	 * @time 2017年3月30日 下午4:41:52
	 * @version v0.1.0
	 * @param userId
	 * @return
	 */
    public String queryByUserId(@Param("userId")Long userId) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT ")
			.append(" CASE WHEN NOW() > date_format(a.activity_end_date,'%Y-%m-%d %H:%i') THEN '亲，活动已过期' ELSE '' End `STATUS`, ")
			.append(" CASE WHEN UPPER(au.audit_status) = 'W' THEN '待审核' WHEN au.cancel_time IS NOT NULL THEN '已取消' WHEN UPPER(au.audit_status) = 'P' THEN '已通过' ELSE '未通过' END audit_status, au.* ")
			.append(" FROM B_ACTIVITY_USER au")
			.append(" LEFT JOIN B_ACTIVITY a ON a.id = au.activity_id ")
			.append(" WHERE au.user_id = #{userId} ");
		return sql.toString();
	}
    
    /**
	 * 根据userId查询活动列表
	 * @author chen.liangzhen
	 * @time 2017年3月30日 下午4:41:52
	 * @version v0.1.0
	 * @param userId
	 * @return
	 */
    public String queryByUserIdAndStatus(@Param("userId")Long userId, @Param("auditStatus")String auditStatus, 
    		@Param("status")String status, @Param("pageNo")int pageNo, @Param("pageSize")int pageSize) {
    	StringBuilder sql = new StringBuilder();
		sql.append("SELECT ")
			.append(" CASE WHEN au.cancel_time IS NOT NULL THEN '已取消' WHEN NOW() > date_format(a.activity_end_date,'%Y-%m-%d %H:%i') THEN '亲，活动已过期' ELSE '未过期' End `STATUS`, ")
			.append(" CASE WHEN UPPER(au.audit_status) = 'W' THEN '待审核' WHEN au.cancel_time IS NOT NULL THEN '已取消' WHEN UPPER(au.audit_status) = 'P' THEN '已通过' ELSE '未通过' END audit_status, ")
			.append(" au.*, a.name activityName, a.cover_pic_url ")
			.append(" FROM B_ACTIVITY_USER au")
			.append(" LEFT JOIN B_ACTIVITY a ON a.id = au.activity_id ")
			.append(" WHERE au.user_id = #{userId} ");
		if(StringUtils.isNotBlank(auditStatus)) {
			sql.append(" AND au.audit_status = '" + auditStatus + "' ");
			sql.append(" AND NOW() <= date_format(a.activity_end_date,'%Y-%m-%d %H:%i') ");
		}
		if(StringUtils.isNotBlank(status) && ActivityStatusEnum.OVERDUE.getValue().equals(status)) {
				sql.append(" AND (NOW() > date_format(a.activity_end_date,'%Y-%m-%d %H:%i') or au.audit_status='C') ");	
		}
		sql.append(" order by au.id desc,IFNULL(au.audit_time,au.cancel_time) desc ");
        sql.append(generatePageSql(pageSize, pageNo));
		
		return sql.toString();
    }
}
